﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameCoder.SqliteDAL;
using GameCoder.Model;

namespace GameCoder.DAL
{
    public class GroupDAL
    {
        public  static GroupItem Add(int id,string name)
        {
            using (GameContent db=GameContent.Instance)
            {
                var res = from g in db.Groups
                          where g.Name == name && g.GameID==id
                          select g;
                if (res.Count()>0)
                {
                    throw new Exception("groups Exist");
                }
                Group group = new Group { ID=GetMaxID()+1,Name=name,GameID=id };
                db.Groups.InsertOnSubmit(group);
                db.SubmitChanges();
                return new GroupItem { ID = group.ID, Name = group.Name };
            }
        }
        private static int GetMaxID()
        {
            using (GameContent db = GameContent.Instance)
            {
                var res = from c in db.Groups
                          select c.ID;
                if (res.Count() == 0)
                {
                    return 0;
                }
                return res.Max();
            }
        }
        public static void Delete(int id)
        {
            using (GameContent db = GameContent.Instance)
            {
                var res = from g in db.Groups
                          where g.ID == id
                          select g;
                if (res.Count() == 0)
                {
                    throw new Exception("No such groups");
                }
                db.Groups.DeleteOnSubmit(res.Single());
                db.SubmitChanges();                
            }
        }
    }
}
